﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.DirectX;
using BurningSphere.Simulator.Properties;
using Microsoft.DirectX.Direct3D;

namespace BurningSphere.Simulator
{

	public class Ray3D 
	{
		public Vector3 Point;
		public Vector3 Direction;
  
		public Ray3D(Vector3 p, Vector3 v)
		{
			this.Point = p;
			this.Direction = v;
		}
	}

	public class Sphere3D 
	{
		public Vector3 Center;
		public float Radius;
  
		public Sphere3D(Vector3 center, float radius)
		{
			this.Center = center;
			this.Radius = radius;
		}
	}

	public static class GeometryHelper
	{
		public static float Intersection(Ray3D r, Sphere3D s)
		{
			// We solve this second-degree equation in t:
			// distance(p+t*v,center)==radius
			// If we define w = p-center
			// we can write that as
			// <w+t*v,w+t*v> == radius*radius
			// <w,w> + 2.0f*t*<w,v> + t*t*<v,v> - radius*radius == 0
			// <v,v>*t*t + 2.0f*<w,v>*t + <w,w>-radius*radius == 0
			// A*t*t + B*t*t + C*t*t == 0
			Vector3 w = r.Point - s.Center;
			float A = Vector3.Dot(r.Direction,r.Direction);
			float B = 2*Vector3.Dot(w,r.Direction);
			float C = Vector3.Dot(w,w) - s.Radius*s.Radius;
			float D = B*B-4.0f*A*C;
			return D >= 0.0f ? (-B - (float)Math.Sqrt(D))/(2.0f*A) : float.PositiveInfinity;
		}
	}
}